package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishesMapper;
import com.sky.mapper.FlavorsMapper;
import com.sky.result.PageResult;
import com.sky.service.DishesService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishesServiceImpl implements DishesService {
    @Autowired
    private DishesMapper dishesMapper;
    @Autowired
    private FlavorsMapper flavorsMapper;

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        List<Dish> dishList = dishesMapper.page(dishPageQueryDTO);
        Page<Dish> page = (Page<Dish>) dishList;
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void addDishes(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishesMapper.addDishes(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        for (DishFlavor dishFlavor : flavorList) {
            dishFlavor.setDishId(dishId);
        }
        flavorsMapper.addFlavors(flavorList);
    }

    @Override
    public void deleteDishes(List<Integer> ids) {
        dishesMapper.deleteDishes(ids);
    }

    @Override
    public void updateDishes(DishDTO dishDTO) {
        LocalDateTime now = LocalDateTime.now();
        dishesMapper.updateDishes(dishDTO,now);
    }

    @Override
    public DishVO getById(Integer id) {
        Dish byId = dishesMapper.getById(id);
        List<DishFlavor> df = flavorsMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(byId,dishVO);
        dishVO.setFlavors(df);
        return dishVO;
    }

    @Override
    public List<DishVO> findByList(Integer categoryId) {
        return dishesMapper.findByList(categoryId);
    }

    @Override
    public void updateStatus(Integer status, Integer id) {
        dishesMapper.updateStatus(status,id);
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> list = dishesMapper.list(dish);

        List< DishVO> dishVOList = new ArrayList<>();
         for (Dish d : list) {
              DishVO dishVO = new DishVO();
              BeanUtils.copyProperties(d,dishVO);
              List<DishFlavor> flavors = flavorsMapper.getByDishId(d.getId());
              dishVO.setFlavors(flavors);
              dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
